package com.neouton.crm.generalPayRequest.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neouton.common.util.AuthUtils;
import com.neouton.common.util.PageUtil;
import com.neouton.config.ResponseAPI;
import com.neouton.crm.entity.SysUserEntity;
import com.neouton.crm.generalPayRequest.entity.GeneralPayRequest;
import com.neouton.crm.generalPayRequest.service.GeneralPayRequestService;
import com.neouton.crm.pm.service.ProjectMemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/generalPayRequest")
public class GeneralPayRequestController {

    @Resource
    private GeneralPayRequestService generalPayRequestService;
    @Resource
    private ProjectMemberService projectMemberService;

    /**
     * 列表信息查询
     *
     */
    @PostMapping("/queryGeneralPayRequestList/{pageNum}/{pageSize}")
    public ResponseAPI queryGeneralPayRequestList(@PathVariable(value = "pageNum") Integer pageNum,
                                        @PathVariable(value = "pageSize") Integer pageSize,
                                        @RequestBody GeneralPayRequest managerPayRequest,
                                        @RequestHeader("token") String token) {
        List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(),"generalPayRequest", token);
        IPage<GeneralPayRequest> page = new Page<>(pageNum, pageSize, true);
        PageUtil<GeneralPayRequest> list = generalPayRequestService.queryGeneralPayRequestList(page, managerPayRequest, userList, token);
        return ResponseAPI.successData(list);
    }

    /**
     * 列表信息查询
     *
     */
    @PostMapping("/queryGeneralPayRequestApprovalList/{pageNum}/{pageSize}")
    public ResponseAPI queryGeneralPayRequestApprovalList(@PathVariable(value = "pageNum") Integer pageNum,
                                                  @PathVariable(value = "pageSize") Integer pageSize,
                                                  @RequestBody GeneralPayRequest managerPayRequest,
                                                  @RequestHeader("token") String token) {
        IPage<GeneralPayRequest> page = new Page<>(pageNum, pageSize, true);
        PageUtil<GeneralPayRequest> list = generalPayRequestService.queryGeneralPayRequestApprovalList(page, managerPayRequest, token);
        return ResponseAPI.successData(list);
    }


    @PostMapping("/saveGeneralPayRequest")
    public ResponseAPI saveManagerPayRequest(@RequestBody GeneralPayRequest managerPayRequest,@RequestHeader("token") String token) {
        return ResponseAPI.successData(generalPayRequestService.saveGeneralPayRequest(managerPayRequest,token));
    }

    @PostMapping("/queryGeneralPayRequestMoney")
    public ResponseAPI queryManagerPayRequestMoney(@RequestBody GeneralPayRequest managerPayRequest) {
        return ResponseAPI.successData(generalPayRequestService.queryGeneralPayRequestMoney(managerPayRequest));
    }

    @PostMapping("/approvedGeneralPayRequest/")
    public ResponseAPI approvedManagerPayRequest(@RequestBody GeneralPayRequest entity, @RequestHeader("token") String token) {
        return ResponseAPI.successData(generalPayRequestService.approvedGeneralPayRequest(entity,token,false));
    }

    @PostMapping("/rejectionGeneralPayRequest")
    public ResponseAPI rejectionManagerPayRequest(@RequestBody GeneralPayRequest entity, @RequestHeader("token") String token) {
        return ResponseAPI.successData(generalPayRequestService.rejectionGeneralPayRequest(entity,token));
    }

    @PostMapping("/queryGeneralPayRequestHistory/{approvalId}")
    public ResponseAPI queryManagerPayRequestHistory(@PathVariable(value = "approvalId") Integer approvalId) {
        return ResponseAPI.successData(generalPayRequestService.queryGeneralPayRequestHistory(approvalId));
    }




    @PostMapping("/deletePayedRequestById/{payRequestId}")
    public ResponseAPI deletePayedRequestById(@PathVariable(value = "payRequestId") Integer payRequestId) {
        Boolean flag= generalPayRequestService.deletePayedRequestById(payRequestId);
        return ResponseAPI.successData(flag);
    }
    /**
     * 导出Excel
     * @param response
     * @param entity
     * @throws Exception
     */
    @PostMapping("/exportInfo")
    public void exportInfo(HttpServletResponse response, @RequestBody GeneralPayRequest entity, @RequestHeader("token") String token) throws Exception{
        List<SysUserEntity> userList = projectMemberService.queryUserList(AuthUtils.getUserId(),"generalPayRequest", token);
        List<GeneralPayRequest> list = generalPayRequestService.queryPaymentRequestListInfo(entity, userList, token);
        generalPayRequestService.exportPaymentRequestInfo(response,list,token);
    }

}
